<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Criteria
 *
 * @author Fahmi Zulhasymi
 */
class Criteria {
    
    private static $spesification;
    private static $order;
    private static $select;
    
    private $query;
    
    
    public function bindingQuery($query){
        //$query = new Dao();
        if(is_array(self::$spesification)){
        foreach(self::$spesification as $spesification){
            switch ($spesification['type']){
                case 'basic':
                    $query = $query->where($spesification['column'], $spesification['operator'], $spesification['value'], 'and');
                    break;
                case 'or':
                    $query = $query->orWhere($spesification['column'], $spesification['operator'], $spesification['value']);
                    break;
                case 'and':
                    $query = $query->where($spesification['column'], $spesification['operator'], $spesification['value'], $spesification['boolean']);
                    break;
                case 'in':
                    $query = $query->whereIn($spesification['column'], $query, $spesification['boolean'], $not);
                    break;
                case 'between':
                    $query = $query->whereBetween($spesification['column'], $spesification['value'], $spesification['boolean'], $not);
                    break;
                case 'lain':
                    break;
                default:
                    break;
            }
        }
        
        if(is_array(self::$order)){
        foreach(self::$order as $order){
            $query = $query->orderBy($order['column']);
            }
        }
        //where 
        //$this->query->whereDay($column, $operator, $value, $boolean);
        //$this->query->whereMonth($column, $operator, $value, $boolean);
        //$this->query->whereExists($callback, $boolean, $not);
        //$this->query->whereHas($relation, $callback, $operator, $count);
        //$this->query->whereNested($callback, $boolean);
        //$this->query->whereNotBetween($column, $values, $boolean);
        //$this->query->whereNotExists($callback, $boolean);
        //$this->query->whereNotIn($column, $query, $boolean);
        //$this->query->whereNotNull($column, $boolean);
        //$this->query->whereNull($column);
        //$this->query->whereYear($column, $operator, $value, $boolean);
        //$this->query->whereRaw($sql, $bindings, $boolean);
        //$this->query->orHas($relation, $operator, $count);
        //$this->query->orWhere($column, $operator, $value);
        //$this->query->orHaving($column, $operator, $value);
        }
        return $query;
    }
        
    public static function equalSpesification($column,$value,$operator='='){
        $type = 'basic';
        self::$spesification[] = compact('type','column','value','operator');
    }
    
    public static function orSpesification($column,$value,$operator='='){
        $type = 'or';
        self::$spesification[] = compact('type','column','value','operator');
    }

    public static function andSpesification($column,$value,$operator='='){
        $type = 'basic';
        self::$spesification[] = compact('type','column','value','operator');
    }
    
    public static function inSpesification($column, $value, $boolean=true){
        $type = 'in';
        self::$spesification[] = compact('type','column','value','boolean');
    }
    
    public static function beetweenSpesification($column, $values, $boolean=true){
        $type = 'between';
        self::$spesification[] = compact('type','column','values','boolean');
    }
    
    public static function orderSpesification($column, $direction='asc'){
        self::$order[] = compact('column','direction');
    }
    
    
}
